package edu.ntu.im.p2ptrade.msg.handle;

import java.util.ArrayList;
import java.util.List;

import edu.ntu.im.p2ptrade.P2PTrade;
import edu.ntu.im.p2ptrade.TransactionManager;
import edu.ntu.im.p2ptrade.entity.Item;
import edu.ntu.im.p2ptrade.entity.Transaction;
import edu.ntu.im.p2ptrade.entity.TransactionRequest;
import edu.ntu.im.p2ptrade.msg.TRequest;
import net.jxta.endpoint.Message;
import net.jxta.endpoint.MessageElement;
import net.jxta.endpoint.Message.ElementIterator;

public class TRequestHandle implements PMessageHandle {
	
	public void handle(Message msg) {
		String transactionId = msg.getMessageElement(P2PTrade.getInstance().getAppName(), TRequest.TRANSACTIONID).toString();
		String part1Peer = msg.getMessageElement(P2PTrade.getInstance().getAppName(), TRequest.PART1OWNER).toString();
		ElementIterator itr = msg.getMessageElements(P2PTrade.getInstance().getAppName(), TRequest.PART1IID);
		List<Item> items1= new ArrayList<Item>();
		while(itr.hasNext()){
			Item item = new Item(part1Peer, null);
			MessageElement mg = itr.next();
			item.setId(Integer.parseInt(mg.toString()));
			items1.add(item);
		}
		ElementIterator itr2 = msg.getMessageElements(P2PTrade.getInstance().getAppName(), TRequest.PART1INAME);
		Integer index = 0;
		while (itr2.hasNext()){
			MessageElement mg = itr2.next();
			items1.get(index++).setName(mg.toString());
			System.out.println("name:"+mg.toString());
		}
		ElementIterator itr3 = msg.getMessageElements(P2PTrade.getInstance().getAppName(), TRequest.PART1IMEMO);
		Integer index2 = 0;
		while (itr3.hasNext()){
			MessageElement mg = itr3.next();
			items1.get(index2++).setMemo(mg.toString());
			System.out.println("memo:"+mg.toString());
		}
		ElementIterator itr4 = msg.getMessageElements(P2PTrade.getInstance().getAppName(), TRequest.PART1PICURL);
		Integer index3 = 0;
		while (itr4.hasNext()){
			MessageElement mg = itr4.next();
			items1.get(index3++).setPhotoUrl(mg.toString());
			System.out.println("picURL:"+mg.toString());
		}
		String part2Peer = msg.getMessageElement(P2PTrade.getInstance().getAppName(), TRequest.PART2OWNER).toString();
		ElementIterator itr5 = msg.getMessageElements(P2PTrade.getInstance().getAppName(), TRequest.PART2IID);
		List<Item> items2= new ArrayList<Item>();
		while(itr5.hasNext()){
			Item item = new Item(part2Peer, null);
			MessageElement mg = itr5.next();
			item.setId(Integer.parseInt(mg.toString()));
			items2.add(item);
		}
		ElementIterator itr6 = msg.getMessageElements(P2PTrade.getInstance().getAppName(), TRequest.PART2INAME);
		Integer index4 = 0;
		while (itr6.hasNext()){
			MessageElement mg = itr6.next();
			items2.get(index4++).setName(mg.toString());
		}
		ElementIterator itr7 = msg.getMessageElements(P2PTrade.getInstance().getAppName(), TRequest.PART2IMEMO);
		Integer index5 = 0;
		while (itr7.hasNext()){
			MessageElement mg = itr7.next();
			items2.get(index5++).setMemo(mg.toString());
		}
		ElementIterator itr8 = msg.getMessageElements(P2PTrade.getInstance().getAppName(), TRequest.PART2PICURL);
		Integer index6 = 0;
		while (itr8.hasNext()){
			MessageElement mg = itr8.next();
			items2.get(index6++).setPhotoUrl(mg.toString());
		}
		
		TransactionRequest tr = new TransactionRequest();
		Transaction trans = new Transaction();
		trans.setId(transactionId);
		Transaction.TradeInfo part1 = new Transaction.TradeInfo(part1Peer);
		part1.setOwner(part1Peer);
		part1.setItems(items1);
		
		Transaction.TradeInfo part2 = new Transaction.TradeInfo(part2Peer);
		part2.setOwner(part2Peer);
		part2.setItems(items2);
		
		trans.setPart1(part1);
		trans.setPart2(part2);
		tr.setTrans(trans);
		
//		tr.setPeer(part1Peer.equals(P2PTrade.getInstance().getPeerName())?part2Peer:part1Peer);
		
		TransactionManager.getInstance().addTransactionRequest(tr);
	}

}
